//
//  StringArrayScreenShowView.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/8/14.
//

#import "StringArrayScreenShowView.h"
#import "UIScrollView+EmptyDataSet.h"

@interface StringArrayScreenShowView ()<UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>

@property (nonatomic ,strong) UIView * makeView;
@property (nonatomic ,assign) CGFloat viewH;
@property (nonatomic ,strong) NSString * SeledString;
@property (nonatomic ,strong) NSArray<NSString *> * stringArray;
@property (nonatomic ,strong) NSArray<NSString *> * SeledstringArray;
@property (nonatomic ,strong) void(^block)(NSString * SeledString);
@property (nonatomic ,strong) NSString * ssStr;

@property (nonatomic ,strong) UITableView * tableview;

@end

@implementation StringArrayScreenShowView

- (NSArray<NSString *> *)SeledstringArray{
    if(self.ssStr.length > 0){
        NSMutableArray * array = [NSMutableArray array];
        for (NSString * str in self.stringArray) {
            if([str containsString:self.ssStr]){
                [array addObject:str];
            }
        }
        return array;
    }
    return self.stringArray;
}
+ (instancetype)showviewWithSeledString:(NSString *)SeledString andStringArray:(NSArray<NSString *>*)stringArray andRightBlock:(void(^)(NSString * SeledString))block{
    StringArrayScreenShowView * view = [StringArrayScreenShowView new];
    view.SeledString = SeledString;
    view.stringArray = stringArray;
    view.block = block;
    [view setupview];
    return view;
}
- (void)setupview{
    [[DTKTool getCurrentVC].view addSubview:self];
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    self.viewH = kwt(500);
    UIView * makeView = [UIView new];
    self.makeView = makeView;
    makeView.backgroundColor = KcolorHex(@"#F3F6F9");
    makeView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH,  self.viewH);
    [self addSubview:makeView];
    [DTKTool BFYJ:kwt(15) andView:makeView andCorner:UIRectCornerTopLeft|UIRectCornerTopRight andSize: CGRectMake(0, 0 ,SCREEN_WIDTH,  self.viewH)];
    
    UILabel * titlab = [DTKTool labelWithText:@"模版名称" andFont:kCFont(18) andTextColor:KcolorHex(@"#242729")];
    [makeView addSubview:titlab];
    [titlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(15));
        make.top.offset(kwt(15));
    }];
    
    UIButton * btn = [UIButton buttonWithType:0];
    [btn setImage:[UIImage imageNamed:@"delete"] forState:UIControlStateNormal];
    btn.addTarget(self,@selector(dismiss));
    [makeView addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-15));
        make.width.height.offset(kwt(20));
        make.centerY.equalTo(titlab);
    }];
    
    MJWeakSelf
    
    UIView * topview = [UIView new];
    topview.backgroundColor = UIColor.whiteColor;
    [makeView addSubview:topview];
    [topview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.offset(kwt(50));
        make.height.offset(kwt(65));
    }];
    
    
    UIView * btnview = [UIView new];
    btnview.addClickBlock(^{
        //        [weakSelf openSsbtnview];
    });
    [DTKTool YJBK:kwt(5) andView:btnview andWidth:1 andColor:KcolorHex(@"#D4D5DA")];
    [topview addSubview:btnview];
    [btnview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(10));
        make.right.offset(kwt(-10));
        make.top.offset(kwt(11));
        make.height.offset(kwt(40));
    }];
    
    UIImageView * btnviewimg = kimageView(@"program_search");
    [btnview addSubview:btnviewimg];
    [btnviewimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(15));
        make.centerY.offset(0);
        make.height.width.offset(kwt(16));
    }];
    
    UITextField * textField = [UITextField new];
    textField.backgroundColor = UIColor.whiteColor;
    textField.attributedPlaceholder = [DTKTool AttributedStringWithStr:@"请搜索" andNodeColor:KcolorHex(@"#89908D") andSeledColor:KcolorHex(@"#89908D") andFont:kFont(16) andPattenr:@""];
    textField.font = kFont(16);
    textField.returnKeyType = UIReturnKeySearch;

    textField.textColor = KcolorHex(@"#222222");
    [btnview addSubview:textField];
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(40));
        make.right.offset(kwt(-65));
        make.centerY.offset(0);
        make.height.offset(kwt(23));
    }];
    [textField addTarget:self action:@selector(textFieldDidChangeValue:)  forControlEvents:UIControlEventEditingChanged];
    
    
    UIView * btnline = [UIView new];
    btnline.backgroundColor = KcolorHex(@"#EAEBED");
    [btnview addSubview:btnline];
    [btnline mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(280));
        make.centerY.offset(0);
        make.width.offset(0.5);
        make.height.offset(kwt(28));
    }];
    
    UILabel * btnviewsslab = [DTKTool labelWithText:@"搜索" andFont:kCFont(16) andTextColor:KcolorHex(@"#0063FC")];
    [btnview addSubview:btnviewsslab];
    [btnviewsslab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-16));
        make.centerY.offset(0);
    }];
    
    
    UIView * topline = [UIView new];
    topline.backgroundColor = KcolorHex(@"#E7E7E7");
    [topview addSubview:topline];
    [topline mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(kwt(2));
        make.bottom.left.right.offset(0);
        
    }];
    
    
    
    UITableView * tableview = [UITableView new];
    tableview.delegate = self;
    tableview.dataSource = self;
    tableview.emptyDataSetSource = self;
    tableview.emptyDataSetDelegate = self;
    tableview.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.01)];
    tableview.sectionHeaderHeight = 0.01;
    tableview.backgroundColor = UIColor.whiteColor;
    [makeView addSubview:tableview];
    self.tableview = tableview;
    [tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.offset(0);
        make.top.equalTo(topline.mas_bottom);
    }];
    
    [self show];
    
}
- (void)textFieldDidChangeValue:(UITextField *)sender{
    if(sender.markedTextRange == nil){
        self.ssStr = sender.text;
        [self.tableview reloadData];
    }
}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView{ return YES; }
- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView {
    if(self.ssStr.length > 0){
        
        //加入你自定义的view
        UIView * noView = [UIView new];
        UIView * NewnoView = [UIView new];
        //    NewnoView.backgroundColor = UIColor.whiteColor;
        [noView addSubview:NewnoView];
        [NewnoView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_offset(KSCREEN_WIDTH);
            make.center.offset(0);
            make.height.mas_offset(KSCREEN_HEIGHT);
        }];
        UIImageView * noImage = [UIImageView new];
        [noView addSubview:noImage];
        noImage.image = [UIImage imageNamed:@"no_search"];
        [noImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_offset(0);
            make.centerY.mas_offset(-100);
        }];
        
        UILabel * noTitleLab = [UILabel new];
        [noView addSubview:noTitleLab];
        noTitleLab.text = @"暂无搜索记录\n换个关键词试试～";
        noTitleLab.numberOfLines = 2;
        noTitleLab.font = kFont(16);
        noTitleLab.textAlignment = NSTextAlignmentCenter;
        noTitleLab.textColor = [UIColor colorWithHexString:@"#44454C"];
        [noTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(noImage.mas_bottom).offset(30);
            make.centerX.mas_offset(0);
        }];
        
        return noView;
    } else{
        return [UIView new];
    }
}
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.SeledstringArray.count;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kwt(52);
    
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    MJWeakSelf
    
    StringArrayScreenTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"StringArrayScreenTableViewCell"];
    if (cell == nil) {
        cell = [[StringArrayScreenTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"StringArrayScreenTableViewCell"];
    }
    cell.name = self.SeledstringArray[indexPath.row];
    cell.isSeled = [self.SeledstringArray[indexPath.row] isEqualToString:self.ssStr];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell ;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(self.block){
        self.block(self.SeledstringArray[indexPath.row]);
        [self dismiss];
    }
    
}
- (void)show{
    [[DTKTool getCurrentVC].view endEditing:YES];
    [UIView animateWithDuration:0.25 animations:^{
        self.makeView.frame = CGRectMake(0, SCREEN_HEIGHT - self.viewH , SCREEN_WIDTH, self.viewH);
    } completion:^(BOOL finished) {
    }];
}
- (void)noclick{}
- (void)dismiss{
    [UIView animateWithDuration:0.25 animations:^{
        self.makeView.frame = CGRectMake(0, SCREEN_HEIGHT , SCREEN_WIDTH, self.viewH);
    } completion:^(BOOL finished) {
        [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        [self removeFromSuperview];
    }];
}

@end



@interface StringArrayScreenTableViewCell ()
@property (nonatomic ,strong) UILabel * namelab;

@end

@implementation StringArrayScreenTableViewCell



- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        [self setupView];
    }
    return self;
}


- (void)setupView{
    self.clipsToBounds = YES;
    self.backgroundColor = UIColor.clearColor;
    self.contentView.backgroundColor = UIColor.whiteColor;
    self.userInteractionEnabled = YES;
    self.contentView.userInteractionEnabled = YES;
    
    UILabel * namelab = [DTKTool labelWithText:@"-" andFont:kFont(16) andTextColor:KcolorHex(@"#242729")];
    self.namelab = namelab;
    [self.contentView addSubview:namelab];
    [namelab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.offset(0);
    }];
    
    
    UIView * line = [UIView new];
    line.backgroundColor = KcolorHex(@"#DEE0E2");
    [self.contentView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.offset(0);
        make.height.offset(0.5);
    }];
}
-(void)setName:(NSString *)name {
    self.namelab.text = name;
}
- (void)setIsSeled:(BOOL)isSeled{
    self.namelab.textColor = isSeled?KcolorHex(@"#0063FC"):KcolorHex(@"#242729");
    self.namelab.font = isSeled?kCFont(17):kFont(16);
}
@end
